package com.sh.data.engine.application.service.Isfd;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.sh.data.engine.application.model.dto.Isfd.IsfdModelDTO;
import com.sh.data.engine.application.model.dto.Isfd.IsfdModelUploadDTO;
import com.sh.data.engine.application.model.dto.Isfd.IsfdStaticStrengthAnalysisMatDTO;
import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.mapper.Isfd.IsfdModelMapper;
import com.sh.data.engine.application.model.mapper.Isfd.IsfdModelSaveDtoMapper;
import com.sh.data.engine.application.model.mapper.Isfd.StaticStrengthAnalysisMatMapper;
import com.sh.data.engine.application.model.request.Isfd.IsfdModelRequest;
import com.sh.data.engine.application.model.request.Isfd.IsfdstaticStrengthAnalysisRequest;
import com.sh.data.engine.domain.Isfd.domain.IsfdModelDomain;
import com.sh.data.engine.domain.Isfd.domain.StaticStrengthAnalysisMatDomain;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.config.IsfdRequestConfig;
import com.sh.data.engine.domain.feign.FeignIsfdModelPage;
import com.sh.data.engine.domain.feign.FeignstaticStrengthAnalysis;
import com.sh.data.engine.domain.feign.Service.IsfdModelFileUploadService;
import com.sh.data.engine.domain.feign.Service.IsfdTokenService;
import com.sh.data.engine.domain.feign.StaticStrengthAnalysisRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IsfdApplicationService {

    @Autowired
    private IsfdTokenService isfdTokenService;

    @Autowired
    private FeignIsfdModelPage feignIsfdModelPage;

    @Autowired
    private FeignstaticStrengthAnalysis feignstaticStrengthAnalysis;

    @Autowired
    private IsfdModelMapper isfdModelMapper;

    @Autowired
    private StaticStrengthAnalysisMatMapper staticStrengthAnalysisMatMapper;

    @Autowired
    private IsfdRequestConfig isfdRequestConfig;

    @Autowired
    private IsfdModelFileUploadService modelUploadService;

    @Autowired
    private IsfdModelSaveDtoMapper isfdModelSaveDtoMapper;

    public PagerResponse<IsfdModelDTO> getElmModelList(IsfdModelRequest isfdModelRequest) {
        String token = "Bearer " + isfdTokenService.getToken();
        JSONObject map = feignIsfdModelPage.getMap(token, isfdRequestConfig.getClientId(), isfdModelRequest.getModelName(), isfdModelRequest.getModelType(), isfdModelRequest.getBusinessScenarios(), isfdModelRequest.getOrderByColumn(), isfdModelRequest.getIsAsc(), isfdModelRequest.getBeginCreateTime(), isfdModelRequest.getEndCreateTime(), isfdModelRequest.getPageNum(), isfdModelRequest.getPageSize());
        List<IsfdModelDomain> resources = (List<IsfdModelDomain>) map.get("rows");
        List<IsfdModelDomain> result = new ArrayList<>();
        for (int i = 0; i < resources.size(); i++) {
            // 将list中的数据转成json字符串
            String jsonObject = JSON.toJSONString(resources.get(i));
            //将json转成需要的IsfdModelDomain对象
            IsfdModelDomain model = JSONObject.parseObject(jsonObject, IsfdModelDomain.class);
            result.add(model);
        }
        PageResult<IsfdModelDomain> pageResult = new PageResult<>();
        pageResult.setResult(result);
        pageResult.setTotalElements((long) map.getIntValue("total"));
        pageResult.setPageNum(isfdModelRequest.getPageNum());
        pageResult.setPageSize(isfdModelRequest.getPageSize());
        pageResult.setTotalPages((int) (pageResult.getTotalElements() / pageResult.getPageSize() + 1));
        return isfdModelMapper.toPageResponse(pageResult);
    }

    public IsfdModelUploadDTO uploadModelFile(File file, Map<String, String> formParams) {
        return isfdModelSaveDtoMapper.toDTO(modelUploadService.uploadModelFile(file, formParams));
    }

    public PagerResponse<IsfdStaticStrengthAnalysisMatDTO> getStaticStrengthAnalysis(IsfdstaticStrengthAnalysisRequest request) {
        String token = "Bearer " + isfdTokenService.getToken();
        StaticStrengthAnalysisRequest staticStrengthAnalysisRequest = new StaticStrengthAnalysisRequest();
        staticStrengthAnalysisRequest.setName(request.getName());
        staticStrengthAnalysisRequest.setPageNum(request.getPageNum());
        staticStrengthAnalysisRequest.setPageSize(request.getPageSize());
        JSONObject map = feignstaticStrengthAnalysis.getMap(token, isfdRequestConfig.getClientId(), staticStrengthAnalysisRequest);
        List<StaticStrengthAnalysisMatDomain> resources = (List<StaticStrengthAnalysisMatDomain>) map.get("rows");
        List<StaticStrengthAnalysisMatDomain> result = new ArrayList<>();
        for (int i = 0; i < resources.size(); i++) {
            // 将list中的数据转成json字符串
            String jsonObject = JSON.toJSONString(resources.get(i));
            //将json转成需要的IsfdModelDomain对象
            StaticStrengthAnalysisMatDomain model = JSONObject.parseObject(jsonObject, StaticStrengthAnalysisMatDomain.class);
            result.add(model);
        }
        PageResult<StaticStrengthAnalysisMatDomain> pageResult = new PageResult<>();
        pageResult.setResult(result);
        pageResult.setTotalElements((long) map.getIntValue("total"));
        pageResult.setPageNum(request.getPageNum());
        pageResult.setPageSize(request.getPageSize());
        pageResult.setTotalPages((int) (pageResult.getTotalElements() / pageResult.getPageSize() + 1));
        return staticStrengthAnalysisMatMapper.toPageResponse(pageResult);
    }
}
